<?php
class Vnf {

    /**
     * Singleton instance
     *
     * Marked only as protected to allow extension of the class. To extend,
     * simply override {@link getInstance()}.
     *
     * @var Vnf
     */
    protected static $_instance = null;

    protected static $controllerDirectory = './vietnamfriendly/src/net/vietnamfriendly/user/modules/';

    /**
     * Singleton instance
     *
     * @return Zend_Controller_Front
     */
    public static function getInstance() {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }


    /**
     * Constructor
     *
     * Instantiate using {@link getInstance()}; front controller is a singleton
     * object.
     *
     * Instantiates the plugin broker.
     *
     * @return void
     */
    protected function __construct()
    {
        $this->setupAutoLoad();

        // load configuration
        $directory = new Zend_Config_Ini('config/config.ini', 'directory');
        Zend_Registry::set('directory', $directory);
        define("BASE_PATH", $directory->basePath);
        define("SRC_PATH", BASE_PATH . $directory->srcPath);
        define("CSS_PATH", SRC_PATH . $directory->css);
        define("JS_PATH", SRC_PATH . $directory->js);

        // setup database

        // load configuration
        $config = new Zend_Config_Ini('config/config.ini', 'database');
        //Zend_Registry::set('config', $config);

        $db = Zend_Db::factory($config->db->adapter,$config->db->config->toArray());
        Zend_Db_Table::setDefaultAdapter($db);
        Zend_Registry::set('db', $db);

    }

    public function setupAutoLoad() {

        include_once('Zend/Loader/Autoloader.php');

        $loader = Zend_Loader_Autoloader::getInstance();
        $loader->setFallbackAutoloader(true);
        $loader->suppressNotFoundWarnings(false);
    }

    public function buildSite() {
        $path = 'src/net/vietnamfriendly/user/modules';
        $sourceDirHandle = opendir($path);

        $allControlers = array();
        $allModels = array();
        while (FALSE !== ($fileName = readdir($sourceDirHandle)) )
        {
            if ($fileName != "." && $fileName != ".." && strpos($fileName, '.') !== 0)
            {
                if (is_dir($path . "/" . $fileName) && $fileName != "." && $fileName != "..")
                {
                    $allControlers[$fileName] = "$path/$fileName/controllers";
                    //                    echo getcwd() . "/$path/$fileName/models";
                    set_include_path(get_include_path()
                    . PATH_SEPARATOR . "./$path/$fileName/models");
                }
            }
        }

        $frontController = Zend_Controller_Front::getInstance();
        $frontController->setControllerDirectory($allControlers);
        $frontController->throwExceptions(true);

        $frontController->setDefaultModule('index');

        $frontController->dispatch();
    }

    public static function getBlockCms() {
        //        echo "<input type='text' id='cmscookie' name='cmscookie' value='' size='200'>";
        //        try
        $result = array();
        try {
            $result =  Zend_Registry::get('blockcms');
        }catch(Exception $ex){
            Zend_Registry::set('blockcms', array());
        }
        return $result;
        //        Zend_Registry::set('blockcms', array());
    }

    public function startCms() {
        //        echo "<input type='text' id='cmscookie' name='cmscookie' value='' size='200'>";
        try {

            Zend_Registry::set('blockcms', array());
        }catch(Exception $ex){
        }
    }

    public function buildCms() {
        $manageBlockCms = Zend_Registry::get('blockcms');
        //print_r($manageBlockCms);
    }
}